package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Payment;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.PayClassifyMapper;
import cn.wolfcode.crm.mapper.PaymentMapper;
import cn.wolfcode.crm.query.PaymentQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPaymentService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PaymentServiceImpl implements IPaymentService {
    @Autowired
    private PaymentMapper paymentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PayClassifyMapper payClassifyMapper;



    public void save(Payment payment) {
        paymentMapper.insert(payment);

    }

    public void update(Payment payment) {

        paymentMapper.updateByPrimaryKey(payment);


    }

    public List queryForList(PaymentQueryObject qo) {
        return paymentMapper.queryForList(qo);
    }


    public PageResult query(QueryObject qo) {
        int total=paymentMapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        List<Payment> rows=paymentMapper.queryForList(qo);
        return new PageResult(total,rows);
    }


    public BigDecimal allAmount(QueryObject qo) {
       /* int all=0;
        List<Integer> amounts= paymentMapper.allAmount(qo);
        for(Integer amount:amounts){
            all+=amount;
        }
        return all;*/
        PageResult result = query(qo);
        List<Payment> rows = result.getRows();
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (Payment row : rows) {

            BigDecimal amount = row.getAmount();
            totalAmount= totalAmount.add(amount);
        }
        return totalAmount;

    }

    public List<Payment> selectAll() {
        return  paymentMapper.selectAll();
    }


    public void delete(Long id) {
        paymentMapper.deleteByPrimaryKey(id);
    }

}
